package com.powermanager.util;

import android.content.Context;
import android.view.inputmethod.InputMethodSubtype;

import com.powermanager.application.MyApplication;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * Created by lqy on 16/7/1.
 */
public class ControlUtil {
    public  ByteArrayOutputStream byteArrayOutputStream;
    public  OutputStream outputStream;
    static  ControlUtil instance;
    public byte[] bytesAccept = new byte[40];
    private static Context applicationContext;

    private ControlUtil(){
        this.applicationContext = MyApplication.getMyApplicationContext();
    }

    public static ControlUtil getInstance() {
        if (instance==null){
            instance  = new ControlUtil();

            String ipAddress = SPUtil.getSharedStringData(applicationContext,"ipAddress");
            int port = SPUtil.getSharedIntData(applicationContext,"port");

            LogUtil.e("---ip---"+ipAddress+"---port---"+port);

            instance.init(ipAddress,port);
        }
        return instance;
    }

    public  void init(final String ipAddress, final int port) {
        //客户端请求与本机在20006端口建立TCP连接
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket client = new Socket(ipAddress,port);
                    client.setSoTimeout(10000);
                    //获取Socket的输出流，用来发送数据到服务端
                    InputStream is = client.getInputStream();
                    outputStream = client.getOutputStream();
                    //获取Socket的输入流，用来接收从服务端发送过来的数据
//                    BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    boolean flag = true;
                    while (flag) {
                        //发送数据到服务端

                        try {
                            //从服务器端接收数据有个时间限制（系统自设，也可以自己设置），超过了这个时间，便会抛出该异常
                            is.read(bytesAccept);
                            String result = "";
                            for(int i = 0;i<bytesAccept.length;i++){
                                int b = bytesAccept[i] & 0xFF;
                                String v = Integer.toHexString(b);
                                if(v.length()<2){
                                    v = "0"+v;
                                }
                                result   = result +v;
                            }
                            System.out.println("服务端发来的内容:" + result);
                        } catch (SocketTimeoutException e) {
                            System.out.println("Time out, No response");
                        }
                    }
                    is.close();
                    if (client != null) {
                        //如果构造函数建立起了连接，则关闭套接字，如果没有建立起连接，自然不用关闭
                        client.close();
                    }//只关闭socket，其关联的输入输出流也会被关闭

                } catch (IOException e) {
                    e.printStackTrace();
                    LogUtil.e(e.toString());
                }
            }
        }).start();

    }
    public  void sendData(byte[] bytes){
        try {
            outputStream.write(bytes);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public   byte[] intToByteArray(int i) {
        byte[] result = new byte[2];
        //由高位到低位
        result[0] = (byte) ((i>> 8) & 0xFF);
        result[1] = (byte) (i & 0xFF);
        return result;
    }
}
